home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / wwbbs31s.lha / Hydra / misc.c < prev    next >
C/C++ Source or Header  |  1994-06-28  |  16KB  |  599 lines

  1. /*=============================================================================
  2.  
  3.                               HydraCom Version 1.00
  4.  
  5.                          A sample implementation of the
  6.                    HYDRA Bi-Directional File Transfer Protocol
  7.  
  8.                              HydraCom was written by
  9.                    Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT
  10.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  11.  
  12.                        The HYDRA protocol was designed by
  13.                  Arjen G. Lentz, LENTZ SOFTWARE-DEVELOPMENT and
  14.                              Joaquim H. Homrighausen
  15.                   COPYRIGHT (C) 1991-1993; ALL RIGHTS RESERVED
  16.  
  17.  
  18.   Revision history:
  19.   06 Sep 1991 - (AGL) First tryout
  20.   .. ... .... - Internal development
  21.   11 Jan 1993 - HydraCom version 1.00, Hydra revision 001 (01 Dec 1992)
  22.  
  23.  
  24.   For complete details of the Hydra and HydraCom licensing restrictions,
  25.   please refer to the license agreements which are published in their entirety
  26.   in HYDRACOM.C and LICENSE.DOC, and also contained in the documentation file
  27.   HYDRACOM.DOC
  28.  
  29.   Use of this file is subject to the restrictions contained in the Hydra and
  30.   HydraCom licensing agreements. If you do not find the text of this agreement
  31.   in any of the aforementioned files, or if you do not have these files, you
  32.   should immediately contact LENTZ SOFTWARE-DEVELOPMENT and/or Joaquim
  33.   Homrighausen at one of the addresses listed below. In no event should you
  34.   proceed to use this file without having accepted the terms of the Hydra and
  35.   HydraCom licensing agreements, or such other agreement as you are able to
  36.   reach with LENTZ SOFTWARE-DEVELOMENT and Joaquim Homrighausen.
  37.  
  38.  
  39.   Hydra protocol design and HydraCom driver:         Hydra protocol design:
  40.   Arjen G. Lentz                                     Joaquim H. Homrighausen
  41.   LENTZ SOFTWARE-DEVELOPMENT                         389, route d'Arlon
  42.   Langegracht 7B                                     L-8011 Strassen
  43.   3811 BT  Amersfoort                                Luxembourg
  44.   The Netherlands
  45.   FidoNet 2:283/512, AINEX-BBS +31-33-633916         FidoNet 2:270/17
  46.   arjen_lentz@f512.n283.z2.fidonet.org               joho@ae.lu
  47.  
  48.   Please feel free to contact us at any time to share your comments about our
  49.   software and/or licensing policies.
  50.  
  51. =============================================================================*/
  52.  
  53. #include "hydracom.h"
  54.  
  55. #ifdef AMIGA
  56. #include <dos/dosextens.h>
  57. #include <clib/dos_protos.h>
  58. #endif    /* AMIGA */
  59.  
  60. static char *chatstart = "\007\007 * Chat mode start\r\n";
  61. static char *chatend   = "\007\007\r\n * Chat mode end\r\n";
  62. static char *chattime  = "\007\007\r\n * Chat mode end - timeout\r\n";
  63.  
  64.  
  65. static void loc_puts (char *s)
  66. {
  67. #ifdef AMIGA
  68. #else
  69.         while (*s) {
  70.               if (*s == '\007')
  71. #if WIN_AGL
  72.                  (void) win_bell();
  73. #else
  74.                  putc(7,stderr);
  75. #endif
  76.               else
  77. #if WIN_AGL
  78.                  win_putc(local_win,*s);
  79. #else
  80.                  putch(*s);
  81. #endif
  82.               s++;
  83.         }
  84. #endif    /* AMIGA */
  85. }/*loc_puts()*/
  86.  
  87.  
  88. int keyabort (void)
  89. {
  90. #define CHATLEN 256
  91.         static byte     chatbuf1[CHATLEN + 5],
  92.                         chatbuf2[CHATLEN + 5],
  93.                        *curbuf = chatbuf1;
  94.         static boolean  warned = false;
  95.         boolean         esc = false;
  96.         char           *p;
  97.         word            c;
  98.  
  99.         if (chattimer > 0L) {
  100.            if (time(NULL) > chattimer) {
  101.               chattimer = lasttimer = 0L;
  102.               hydra_devsend("CON",(byte *) chattime,strlen(chattime));
  103.               loc_puts(&chattime[2]);
  104.            }
  105.            else if ((time(NULL) + 10L) > chattimer && !warned) {
  106.               loc_puts("\007\r\n * Warning: chat mode timeout in 10 seconds\r\n");
  107.               warned = true;
  108.            }
  109.         }
  110.         else if (chattimer != lasttimer) {
  111.            if (chattimer ==  0L) {
  112.               if (nobell) p = " * Remote has chat facility with bell disabled\n";
  113.               else        p = " * Remote has chat facility with bell enabled\n";
  114.               hydra_devsend("CON",(byte *) p,(int) strlen(p));
  115.               loc_puts(" * Hydra session in progress, chat facility now available\r\n");
  116.            }
  117.            else if (chattimer == -1L)
  118.               loc_puts(" * Hydra session in init state, can't chat yet\r\n");
  119.            else if (chattimer == -2L)
  120.               loc_puts(" * Remote has no chat facility available\r\n");
  121.            else if (chattimer == -3L) {
  122.               if (lasttimer > 0L) loc_puts("\r\n");
  123.               loc_puts(" * Hydra session in exit state, can't chat anymore\r\n");
  124.            }
  125.            lasttimer = chattimer;
  126.         }
  127.  
  128. #if WIN_AGL
  129.         while (win_keyscan()) {
  130. #else
  131. #ifdef AMIGA
  132.         while (0) {
  133. #else
  134.         while (kbhit()) {
  135. #endif
  136. #endif
  137.               switch (c = get_key()) {
  138.                      case Esc:
  139.                           esc = true;
  140.                           break;
  141.  
  142.                      case Alt_C:
  143.                           if (chattimer == 0L) {
  144.                              hydra_devsend("CON",(byte *) chatstart,strlen(chatstart));
  145.                              loc_puts(&chatstart[2]);
  146.                              chattimer = lasttimer = time(NULL) + CHAT_TIMEOUT;
  147.                           }
  148.                           else if (chattimer > 0L) {
  149.                              chattimer = lasttimer = 0L;
  150.                              hydra_devsend("CON",(byte *) chatend,strlen(chatend));
  151.                              loc_puts(&chatend[2]);
  152.                           }
  153.                           else
  154.                              loc_puts("\007");
  155.                           break;
  156.  
  157.                      default:
  158.                           if (c < ' ' || c > 126)
  159.                              break;
  160.  
  161.                      case '\r':
  162.                      case '\a':
  163.                      case '\b':
  164.                           if (chattimer <= 0L)
  165.                              break;
  166.  
  167.                           chattimer = time(NULL) + CHAT_TIMEOUT;
  168.                           warned = false;
  169.  
  170.                           if (chatfill >= CHATLEN)
  171.                              loc_puts("\007");
  172.                           else {
  173.                              switch (c) {
  174.                                     case '\r':
  175.                                          curbuf[chatfill++] = '\n';
  176.                                          loc_puts("\r\n");
  177.                                          break;
  178.  
  179.                                     case '\b':
  180.                                          if (chatfill > 0 && curbuf[chatfill - 1] != '\n')
  181.                                             chatfill--;
  182.                                          else {
  183.                                             curbuf[chatfill++] = '\b';
  184.                                             curbuf[chatfill++] = ' ';
  185.                                             curbuf[chatfill++] = '\b';
  186.                                          }
  187.                                          loc_puts("\b \b");
  188.                                          break;
  189.  
  190.                                     default:
  191.                                          curbuf[chatfill++] = (byte) c;
  192.                                          if (c != 7)
  193. #if WIN_AGL
  194.                                             win_putc(local_win,c);
  195. #else
  196. #ifdef AMIGA
  197. #else
  198.                                             putch(c);
  199. #endif
  200. #endif
  201.                                          break;
  202.                              }
  203.                           }
  204.                           break;
  205.               }
  206.         }
  207.  
  208.         if (chatfill > 0 && hydra_devsend("CON",curbuf,chatfill)) {
  209.            curbuf = (curbuf == chatbuf1) ? chatbuf2 : chatbuf2;
  210.            chatfill = 0;
  211.         }
  212.  
  213.         return (esc);
  214. }/*keyabort()*/
  215.  
  216.  
  217. void rem_chat (byte *data, word len)
  218. {
  219.         len = len;
  220.  
  221. #if !WIN_AGL && !defined(AMIGA)
  222.         local_x = wherex();
  223.         local_y = wherey();
  224.         window(1,11,80,17);
  225.         gotoxy(remote_x,remote_y);
  226. #endif
  227.  
  228.         while (*data) {
  229.               switch (*data) {
  230.                      case '\a':
  231.                           if (!nobell) {
  232. #if WIN_AGL
  233.                              (void) win_bell();
  234. #else
  235. #ifdef AMIGA
  236. #else
  237.                              putc(7,stderr);
  238. #endif
  239. #endif
  240.                           }
  241.                           break;
  242.  
  243.                      case '\n':
  244. #if WIN_AGL
  245.                           win_putc(remote_win,'\r');
  246. #else
  247. #ifdef AMIGA
  248. #else
  249.                           putch('\r');
  250. #endif
  251. #endif
  252.                           /* fallthrough to default */
  253.  
  254.                      default:
  255. #if WIN_AGL
  256.                           win_putc(remote_win,(int) *data);
  257. #else
  258. #ifdef AMIGA
  259. #else
  260.                           putch((int) *data);
  261. #endif
  262. #endif
  263.                           break;
  264.               }
  265.               data++;
  266.         }
  267.  
  268. #if !WIN_AGL && !defined(AMIGA)
  269.         remote_x = wherex();
  270.         remote_y = wherey();
  271.         window(1,19,80,25);
  272.         gotoxy(local_x,local_y);
  273. #endif
  274. }/*rem_chat()*/
  275.  
  276.  
  277. int parse(char *string)
  278. {
  279.         int ac = 0;
  280.         char *p;
  281.  
  282.         p = strchr(string,';');
  283.         if (p) *p = '\0';
  284. #ifndef AMIGA
  285.         strupr(string);
  286. #endif    /* AMIGA */
  287.         av[ac] = strtok(string," \t\r\n\032");
  288.  
  289.         while (av[ac]) {
  290.               if (++ac > MAXARGS) {
  291.                  message(6,"!Too many arguments!");
  292.                  endprog(2);
  293.               }
  294.               av[ac]=strtok(NULL," \t\r\n\032");
  295.         }
  296.  
  297.         return (ac);
  298. }/*parse()*/
  299.  
  300.  
  301. void splitpath(char *filepath,char *path,char *file)
  302. {
  303. #ifdef AMIGA
  304.     STRPTR path_name,file_name;
  305.     LONG len;
  306.  
  307.     path_name = PathPart(filepath);
  308.     file_name = FilePart(filepath);
  309.  
  310.     strcpy(file,file_name);
  311.  
  312.     len = path_name - filepath;
  313.  
  314.     memcpy(path,filepath,len);
  315.  
  316.     path[len] = 0;
  317. #else
  318.         char *p,*q;
  319.  
  320.         for (p=filepath;*p;p++) ;
  321.         while (p!=filepath && *p!=':' && *p!='\\' && *p!='/') --p;
  322.         if (*p==':' || *p=='\\' || *p=='/') ++p;        /* begin     */
  323.         q=filepath;
  324.         while (q!=p) *path++=*q++;                      /* copy path */
  325.         *path='\0';
  326.         strcpy(file,p);
  327. #endif    /* AMIGA */
  328. }/*splitpath()*/
  329.  
  330.  
  331. void mergepath(char *filepath,char *path,char *file)
  332. {
  333. #ifdef AMIGA
  334.     strcpy(filepath,path);
  335.     AddPart(filepath,file,256);
  336. #else
  337.         strcpy(filepath,path);
  338.         strcat(filepath,file);
  339. #endif    /* AMIGA */
  340. }/*mergepath()*/
  341.  
  342.  
  343. int fexist (char *filename)
  344. {
  345. #ifdef AMIGA
  346.     BPTR file_lock;
  347.  
  348.     if(file_lock = Lock(filename,SHARED_LOCK))
  349.     {
  350.         UnLock(file_lock);
  351.  
  352.         return(1);
  353.     }
  354.     else
  355.         return(0);
  356. #else
  357.         struct stat f;
  358.  
  359.         return ((stat(filename,&f) != -1) ? 1 : 0);
  360. #endif    /* AMIGA */
  361. }/*fexist()*/
  362.  
  363.  
  364. int get_key (void)
  365. {
  366. #ifdef AMIGA
  367.         return(0);
  368. #else
  369. #if WIN_AGL
  370.         if (didsome)
  371.            return (win_keygetc());
  372.         else
  373. #endif
  374.              {
  375.            register int c = getch();
  376.  
  377.            return (c ? c : getch() | 0x100);
  378.         }
  379. #endif    /* AMIGA */
  380. }/*get_key()*/
  381.  
  382.  
  383. void any_key (void)
  384. {
  385.         fprintf(stderr,"Press any key to continue");
  386.         get_key();
  387.         fprintf(stderr,"\r                          \r");
  388. }/*any_key()*/
  389.  
  390.  
  391. int get_str (char *prompt, char *s, int maxlen)
  392. {
  393.         int i = (int) strlen(s),c;
  394.  
  395.         cprint("\r%s: %s",prompt,s);
  396.         for (;;) {
  397. #ifdef AMIGA
  398.             sys_idle();
  399.             continue;
  400. #endif    /* AMIGA */
  401.         }/*for*/
  402. }/*get_str()*/
  403.  
  404.  
  405. void resultlog (boolean xmit, char *fname, long bytes, long xfertime)
  406. {          /* Omen's DSZ compatible logfile - for RBBS-PC XFER-?.DEF reports */
  407.         FILE *fp;
  408.  
  409.         if (opuslog) {
  410.            if ((fp = sfopen(opuslog,"at",DENY_WRITE)) != NULL) {
  411.               if (fname) {
  412.                  fprintf(fp, "%s %s%s %ld", xmit ? "Sent" : "Got",
  413.                              xmit ? "" : download, fname, bytes);
  414.                  if (mailer)
  415.                     fprintf(fp," %ld",xfertime);
  416.                  fprintf(fp,"\n");
  417.               }
  418.               fclose(fp);
  419.            }
  420.            else
  421.               message(3,"-Couldn't append opus log-file %s",opuslog);
  422.         }
  423.  
  424.         if (result) {
  425.            if ((fp = sfopen(result,"at",DENY_WRITE)) != NULL) {
  426.               if (fname) {
  427.                  fprintf(fp, "%c %6ld %5u bps %4ld cps 0 errors     0 1024 %s -1\n",
  428.                              xmit ? 'H' : 'R',
  429.                              bytes, cur_speed,
  430.                              xfertime ? (bytes / xfertime) : 9999L,
  431.                              fname);
  432.               }
  433.               fclose(fp);
  434.            }
  435.            else
  436.               message(3,"-Couldn't append result-file %s",result);
  437.         }
  438. }/*resultlog()*/
  439.  
  440.  
  441. static char *mon[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  442.                          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  443.  
  444. char *h_revdate (long revstamp)
  445. {
  446.         static char  buf[12];
  447.         struct tm   *t;
  448.  
  449.         t = localtime(&revstamp);
  450.         sprintf(buf, "%02d %s %d",
  451.                      t->tm_mday, mon[t->tm_mon], t->tm_year + 1900);
  452.  
  453.         return (buf);
  454. }/*h_revdate()*/
  455.  
  456.  
  457. void message (int level, char *fmt,...)
  458. {
  459.         char       buf[255];
  460.         long       tim;
  461.         struct tm *t;
  462.         va_list    arg_ptr;
  463.  
  464.         tim = time(NULL);
  465.         t = localtime(&tim);
  466.  
  467.         va_start(arg_ptr,fmt);
  468.         sprintf(buf, "%c %02d %03s %02d:%02d:%02d %-4s ",
  469.                      *fmt, t->tm_mday, mon[t->tm_mon],
  470.                      t->tm_hour, t->tm_min, t->tm_sec, LOGID);
  471.         vsprintf(&buf[23], &fmt[1], arg_ptr);
  472.         va_end(arg_ptr);
  473.  
  474.         if (level >= loglevel && logfp)
  475.            fprintf(logfp, "%s\n", buf);
  476.  
  477. #if WIN_AGL
  478.         if (!file_win)
  479.            cprint("\r%s\n",buf);
  480.         else {
  481.            if (!log_first)
  482.               log_first = true;
  483.            else
  484.               win_putc(log_win,'\n');
  485.            win_puts(log_win,buf);
  486.         }
  487. #else
  488. #ifdef AMIGA
  489.            cprint("%s\n",buf);
  490. #else
  491.         if (!file_x)
  492.            cprint("\r%s\n",buf);
  493.         else {
  494.            local_x = wherex();
  495.            local_y = wherey();
  496.            window(1,2,80,6);
  497.            if (log_y) {
  498.               gotoxy(1,log_y);
  499.               putch('\n');
  500.            }
  501.            cputs(buf);
  502.            log_y = wherey();
  503.            window(1,19,80,25);
  504.            gotoxy(local_x,local_y);
  505.         }
  506. #endif
  507. #endif
  508. }/*message()*/
  509.  
  510.  
  511. void cprint (char *fmt, ...)
  512. {
  513.         char    buf[255];
  514.         va_list arg_ptr;
  515.  
  516.         va_start(arg_ptr,fmt);
  517.         vsprintf(buf, fmt, arg_ptr);
  518.         va_end(arg_ptr);
  519.  
  520. #if WIN_AGL
  521.         if (didsome)
  522.            win_puts(0,buf);
  523.         else
  524. #else
  525. #ifdef AMIGA
  526. #endif
  527. #endif
  528.            fputs(buf,stdout);
  529. }/*cprint()*/
  530.  
  531.  
  532. void hydra_gotoxy (int x, int y)
  533. {
  534. #if WIN_AGL
  535.         win_setpos(file_win,x,y);
  536. #else
  537. #ifdef AMIGA
  538. #else
  539.         file_x = x;
  540.         file_y = y;
  541. #endif
  542. #endif
  543. }/*hydra_gotoxy()*/
  544.  
  545.  
  546. void hydra_printf (char *fmt, ...)
  547. {
  548.         char    buf[255];
  549.         va_list arg_ptr;
  550.  
  551.         va_start(arg_ptr,fmt);
  552.         vsprintf(buf, fmt, arg_ptr);
  553.         va_end(arg_ptr);
  554.  
  555. #if WIN_AGL
  556.         win_puts(file_win,buf);
  557. #else
  558. #ifdef AMIGA
  559. #else
  560.         local_x = wherex();
  561.         local_y = wherey();
  562.         window(1,8,80,9);
  563.  
  564.         gotoxy(file_x,file_y);
  565.         cputs(buf);
  566.  
  567.         file_x = wherex();
  568.         file_y = wherey();
  569.         window(1,19,80,25);
  570.         gotoxy(local_x,local_y);
  571. #endif
  572. #endif
  573. }/*hydra_printf()*/
  574.  
  575.  
  576. void hydra_clreol (void)
  577. {
  578. #if WIN_AGL
  579.         win_clreol(file_win);
  580. #else
  581. #ifdef AMIGA
  582. #else
  583.         local_x = wherex();
  584.         local_y = wherey();
  585.         window(1,8,80,9);
  586.  
  587.         gotoxy(file_x,file_y);
  588.         clreol();
  589.  
  590.         file_x = wherex();
  591.         file_y = wherey();
  592.         window(1,19,80,25);
  593.         gotoxy(local_x,local_y);
  594. #endif
  595. #endif
  596. }/*hydra_clreol()*/
  597.  
  598. /* end of misc.c */
  599.